package com.xdkj.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xdkj.beans.Student;
import com.xdkj.service.StudentService;
import com.xdkj.util.BaseResponse;
import com.xdkj.util.BaseState;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2021/11/24 16:14
 * 第一次修改
 */
@Controller
@RequestMapping("first")
@Api(tags = "firstController类")
public class FirstController {
    @Autowired
    StudentService studentService;
    @GetMapping("findOne")
    @ApiOperation(value = "获取全部学生数据", notes = "获取全部学生数据", httpMethod = "GET")
    public String findOne(Model model){
        List<Student> students = studentService.queryAll();
        BaseResponse ok = BaseResponse.ok();
        ok.setCode(BaseState.QUERYSUCCESS.getCode());
        ok.setMassage(BaseState.QUERYSUCCESS.getMassage());
        ok.setData(students);
        model.addAttribute("baseResponse",ok);
        return "list";
    }
    @GetMapping("findTwo")
    @ResponseBody
    @ApiOperation(value = "获取学生信息，封装到BaseResponse中",notes ="获取date",httpMethod = "POST")
    public BaseResponse findTwo(){
        List<Student> students = studentService.queryAll();
        BaseResponse ok = BaseResponse.ok();
        ok.setCode(BaseState.QUERYSUCCESS.getCode());
        ok.setMassage(BaseState.QUERYSUCCESS.getMassage());
        ok.setData(students);
        return ok;
    }
    @GetMapping("queryByPage")
    @ResponseBody
    @ApiOperation(value = "分页查询",notes = "分页查询",httpMethod = "GET")
    public BaseResponse queryByPage(Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Student> list = studentService.queryAll();
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return BaseResponse.ok(2000, "success", pageInfo);
    }
}
